#!/bin/sh
# tools/ccn_run
# 
# Part of the CCNx distribution.
#
# A utility script to start a CCNx java command line utility
#
# Copyright (C) 2008, 2009, 2010 Palo Alto Research Center, Inc.
#
# This work is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License version 2 as published by the
# Free Software Foundation. 
# This work is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details. You should have received a copy of the GNU General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.

JAR_FILE=ccn.jar
JAVA_HOME=${JAVA_HOME:=/usr}
JAVA=$JAVA_HOME/bin/java
SCRIPT_DIR=`dirname $0`  # note this is relative path
CMD_NAME=`basename $0` 
CCN_HOME_JAVA=${CCN_HOME_JAVA:-$SCRIPT_DIR}
if [ ! -f "$CCN_HOME_JAVA/$JAR_FILE" ]
then
    if [ -f "$SCRIPT_DIR/../lib/$JAR_FILE" -o -f "$SCRIPT_DIR/../$JAR_FILE" ] 
    then
	CCN_HOME_JAVA="$SCRIPT_DIR/.."
    else
	echo "Cannot find $JAR_FILE, set CCN_HOME_JAVA env variable."
	exit 1;
    fi
fi

CP1=$CCN_HOME_JAVA/*.jar
CP2=$CCN_HOME_JAVA/lib/*.jar

CP1=`echo $CP1 | sed "s/ /:/g"`
CP2=`echo $CP2 | sed "s/ /:/g"`

CLASSPATH=$CP1:$CP2
PORTARG=

if [ ! "$CCN_LOCAL_PORT" = "" ]; then
  PORTARG="-Dccn.agent.port=$CCN_LOCAL_PORT"
fi

DEBUG_OPTION=
if [ "$1" = "-debug" ]; then
   DEBUG_OPTION="-Xrunjdwp:transport=dt_socket,address=$2,server=y,suspend=n"
   shift 2
fi

if [ $CMD_NAME != "ccn_run" ]; then
    UTIL=$CMD_NAME
elif [ $# = 0 ]
then
  echo Possible commands:
  echo 
  (cd $SCRIPT_DIR; grep -l -I ccn_run * 2>/dev/null) | grep -v ccn_run | sort -u
  exit 1
else
  UTIL=$1
  shift
fi

$JAVA -cp $CLASSPATH $PORTARG $DEBUG_OPTION org.ccnx.ccn.utils.$UTIL $*


